import * as YvanUI from "yvan-ui-wotu";

const _ = YvanUI.lodash

interface Ref {
    theform: YvanUI.Form
}

export default class aliasInsert extends YvanUI.Scope<void, Ref> {

    main = this.$$({
        type: 'symptom',
        standard: '',
        aligns: '',
        guid: ''
    })

    onRender() {
        window['biz'] = this
    }

    initChange() {
        Object.assign(this.main, {
            standard: '',
            aligns: ''
        })
    }

    dataChange() {
        if (!this.main.standard) {
            this.main.aligns = ''
        }
        if (!this.main.type) {
            return
        }

        YvanUI.ajax.func({
            url: '/kg/search/align',
            method: 'post-json',
            data: {
                label: this.main.type,
                standard: this.main.standard
            }
        }).then((res: any) => {
            let ret = res.result.aligns
            const result = ret.join(",")
            Object.assign(this.main, {
                aligns: result
            })
        }).finally(() => {

        })
    }

    vjson = {
        view: 'page',
        title: '症状对齐设置',
        items: [
            {
                view: 'card',
                items: [
                    {
                        view: 'form',
                        debug: 'main',
                        ref: 'theform',
                        labelCol: {span: 6},
                        wrapperCol: {span: 14},
                        items: [
                            {
                                view: 'radio',
                                bind: 'main.type',
                                label: '类型',
                                rules: [{required: true}],
                                optionType: 'button',
                                data: [
                                    {value: 'symptom', label: '症状'},
                                    {value: 'disease', label: '疾病'},
                                    {value: 'medicine', label: '药品'}
                                ],
                                value: {
                                    $watch: () => this.main.type,
                                    $then: () => {
                                        Object.assign(this.main, {
                                            aligns: '',
                                            standard: '',
                                            guid: ''
                                        })
                                    }
                                },
                            },
                            {
                                view: 'select',
                                label: '标准文本',
                                bind: 'main.standard',
                                rules: [{required: true}],
                                value: {
                                    $watch: () => this.main.type,
                                    $then: () => ''
                                },
                                dataSource: {
                                    func: ({type}) => {
                                        return new Promise(resolve => {
                                            if (!this.main.type) {
                                                resolve([])
                                                return
                                            }

                                            YvanUI.ajax.func({
                                                url: '/kg/search/data/info',
                                                method: 'post-json',
                                                data: {
                                                    label: "align",
                                                    name: this.main.type,
                                                    align: true
                                                }
                                            }).then((res: any) => {
                                                const data = _.map(res.result, (i) => {
                                                    return {value: i, label: i}
                                                })

                                                resolve(data)
                                            }).finally(() => {
                                                resolve([])
                                            })
                                        })

                                    },
                                    params: {
                                        type: {$watch: () => this.main.type}
                                    }
                                },
                                onChange: this.dataChange
                            },
                            {
                                view: 'textarea',
                                label: '待处理文本',
                                bind: 'main.aligns',
                                rules: [{required: true}],
                            },
                        ]
                    }
                ]
            }
        ],
        footer: [
            {
                view: 'button',
                text: '保存',
                icon: 'antd.SaveOutlined',
                type: 'primary',
                onClick: () => new Promise((resolve, reject) => {
                    this.refs.theform.validateFields().then(res => {
                        YvanUI.ajax.func({
                            url: '/kg/insert/align',
                            method: 'post-json',
                            data: {
                                label: this.main.type,
                                aligns: this.main.aligns.split(","),
                                standard: this.main.standard
                            }
                        }).then((rest) => {
                            const res: any = rest
                            if (res.code != '0') {
                                YvanUI.showErrorDialog(res.result)
                                reject(res.result)
                            } else {
                                YvanUI.showSuccess('插入成功')
                                Object.assign(this.main, {
                                    guid: res.guid,
                                });
                                resolve()
                            }
                        })

                    }).catch(e => {
                        YvanUI.showValidateErrorDialog(this, e)
                        reject(e)
                    })
                })
            },
            {
                view: 'button',
                text: '同步',
                icon: 'antd.FileSyncOutlined',
                type: 'danger',
                disabled: {
                    $watch: () => !this.main.guid
                },
                onClick: () => new Promise((resolve, reject) => {
                    YvanUI.ajax.func({
                        url: window['wechatAPI'] + '/kg/align/text',
                        method: 'post-json',
                        data: {
                            label: this.main.type,
                            standard: this.main.standard,
                            aligns: this.main.aligns.split(",")
                        }
                    }).then((rest) => {
                        const res: any = rest
                        if (res.code != '0') {
                            YvanUI.showErrorDialog(res.result)
                        } else {
                            YvanUI.showSuccess('同步成功')
                            YvanUI.transaction(() => {
                                Object.assign(this.main, {
                                    guid: '',
                                    standard: '',
                                    aligns: '',
                                    type: ''
                                });
                            })
                        }
                    }).finally(() => {
                        resolve()
                    })
                })
            },
            {
                view: 'button',
                text: '清空',
                icon: 'antd.ClearOutlined',
                onClick: () => {
                    this.refs.theform.resetFields()
                }
            }
        ]
    }
}